package com.sdk.controller;

import com.sdk.bean.CodeInfo;
import com.sdk.bean.MessageBean;
import com.sdk.service.InfluxdbDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/InfluxdbDemo")
public class InfluxdbDemoController {

    @Autowired
    private InfluxdbDemoService influxdbDemoService;

    /**
     * 测试保存
     * @return
     */
    @RequestMapping(value = "/saveMessage",method = {RequestMethod.POST })
    @ResponseBody
    public MessageBean saveMessage(){
        MessageBean messageBean = new MessageBean();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            Map<String, String> tags = new HashMap<String, String>();
            Map<String, Object> fields = new HashMap<String, Object>();
            List<CodeInfo> list = new ArrayList<CodeInfo>();

            CodeInfo info1 = new CodeInfo();
            info1.setId(1L);
            info1.setName("BANKS");
            info1.setCode("ABC");
            info1.setDescr("中国农业银行");
            info1.setDescrE("ABC");
            info1.setCreatedBy("system");
            info1.setCreatedAt(new Date().getTime());

            CodeInfo info2 = new CodeInfo();
            info2.setId(2L);
            info2.setName("BANKS");
            info2.setCode("CCB");
            info2.setDescr("中国建设银行");
            info2.setDescrE("CCB");
            info2.setCreatedBy("system");
            info2.setCreatedAt(new Date().getTime());

            list.add(info1);
            list.add(info2);

            influxdbDemoService.saveMessage(list);

            messageBean.setCode("1");
            messageBean.setType("success");
            messageBean.setMessage("保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setCode("0");
            messageBean.setType("fail");
            messageBean.setMessage("保存失败!");
        }
        return messageBean;
    }


    /**
     * 测试查询
     * @return
     */
    @RequestMapping(value = "/selectMessage",method = {RequestMethod.POST })
    @ResponseBody
    public MessageBean selectMessage(){
        MessageBean messageBean = new MessageBean();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            List<CodeInfo> list = influxdbDemoService.selectMessage();
            dataMap.put("CodeInfoList",list);
            messageBean.setData(dataMap);
            messageBean.setCode("1");
            messageBean.setType("success");
            messageBean.setMessage("查询成功!");
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setCode("0");
            messageBean.setType("fail");
            messageBean.setMessage("查询失败!");
        }
        return messageBean;
    }
}
